<template>
  <div class="login-page">
    <div class="title r-nw-fs-c">
      <img class="block" src="../../assets/images/logo.png" />
      <h1 style="margin-left: 15px; font-weight: bold">{{appTitle}}</h1>
    </div>
    <div class="bg-form">
      <div class="form-bg">
        <h2 style="text-align: center; letter-spacing: 1px; margin-bottom: 20px; color: #fff" clss="form-title">用户登录</h2>
        <el-form :model="loginInfo" :rules="rules" @keydown.enter.native="login('loginForm')" class="form" label-position="left" label-width="0px" ref="loginForm">
          <el-form-item prop="username">
            <el-input autocomplete="off" placeholder="账号" type="text" prefix-icon="el-icon-user-solid" v-model="loginInfo.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input autocomplete="off" placeholder="密码" type="password"  prefix-icon="el-icon-s-goods" v-model="loginInfo.password"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <div class="r-nw-sb-c">
              <el-input style="width: 55%" autocomplete="off" placeholder="验证码" v-model="loginInfo.code"  prefix-icon="el-icon-s-help"></el-input>
              <!-- <img width="35%;" height="32px" @click="changeCode()" :src="codeSrc" alt /> -->
            </div>
          </el-form-item>
          <el-form-item style="width: 100%">
            <el-button class="login" :loading="loading" @click.native.prevent="login('loginForm')" type="primary">登 录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="bottom r-nw-c-c">
      <span @click="isShowModal=true">版本: <span class="version">{{ appTitle }} {{ version }}</span></span>
    </div>
    <el-dialog :visible.sync="isShowModal" title="更新日志" width="800px">
      <update-log></update-log>
    </el-dialog>
  </div>
</template>

<script>
import { set } from 'js-cookie';
import { setStorage } from '@/utils/storage';
import config from '@/config';
import AuthApi from '@/api/auth';
import UpdateLog from './components/UpdateLog.vue';
import {cryptoPassword} from '@/utils';
export default {
  components: { UpdateLog },
  data() {
    // let ruleUserName = (rule, value, callback) => {
    //   AuthApi.userNameIsExist({ username: value }).then((res) => {
    //     if (res === false) {
    //       callback(new Error('用户名不存在'));
    //     } else {
    //       callback();
    //     }
    //   });
    // };
    return {
      loginInfo: {
        username: 'admin',
        password: '123456',
        code: '1234',
        randomStr: ''
      },
      rules: {
        username: [
          {
            type: 'string',
            required: true,
            trigger: 'blur',
            message: '请输入用户名'
          },
          // { validator: ruleUserName }
        ],
        password: [
          {
            type: 'string',
            required: true,
            trigger: 'blur',
            message: '请输入密码'
          }
        ],
        code: [
          {
            type: 'string',
            required: true,
            trigger: 'blur',
            message: '请输入验证码'
          }
        ]
      },
      loading: false,
      appTitle: config.appTitle,
      version: config.appVersion,
      codeSrc: '',
      isShowModal: false
    };
  },
  computed: {
    redirect() {
      let noRedirect = ['/err', '/login'];
      let redirectUrl = this.$route.query.redirect || '/';
      return noRedirect.includes(redirectUrl) ? '/' : redirectUrl;
    }
  },
  methods: {
    // 验证码
    changeCode() {
      let str = new Date().getTime();
      this.loginInfo.randomStr = str;
      this.codeSrc = `${config.baseUrl}/code/` + str;
    },
    // 登录
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          let { username, password, code, randomStr } = this.loginInfo;
          let sign = cryptoPassword(username + ':' + password + ':' + new Date().getTime());
          let params = {
            username,
            password,
            sign,
            scope: 'webapp',
            code,
            randomStr
          };
          AuthApi.login(params)
            .then((res) => {
              this.$message.success('登录成功！');
              set(config.tokenKey, res.access_token);
              set(config.refreshTokenKey, res.refresh_token);
              set(config.tokenExpiresKey, Date.now() + res.expires_in * 1000 - config.refreshAheadTime);
              this.getMenuList();
              this.getUnloadCount();
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
              this.changeCode();
            });
        } else {
          this.loading = false;
          this.$message.error('用户名或密码输入不正确！');
          this.changeCode();
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        return cur !== 'redirect' ? (acc[cur] = query[cur]) : acc;
      }, {});
    },
    // 监听刷新
    listenPage() {
      window.onbeforeunload = function (e) {
        e = e || window.event;
        if (e) {
          this.changeCode();
        }
        return;
      };
    },
    // 获取未读消息
    getUnloadCount() {
      AuthApi.unloadCount().then((res) => setStorage('count', res));
    },
    // 储存菜单及用户信息
    getMenuList() {
      AuthApi.getMenus({}).then((res) => {
        // 存储用户信息
        setStorage('userinfo', res.detail);
        setStorage('permissions', res.btnPermissions);
        // 存储返回的菜单
        setStorage('rawMenu', res.menus);
        this.$router.replace({
          path: this.redirect,
          query: this.getOtherQuery(this.$route.query)
        });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.login-page {
  // width: 100vw;
  height: 100vh;
  .title {
    margin-left: 45px;
    height: 80px;
    letter-spacing: 2px;
    .block {
      width: 30px;
      height: 30px;
    }
  }
  .bg-form {
    position: relative;
    background: url('../../assets/images/login-bg.jpg') center no-repeat;
    height: calc(100vh - 200px);
    .form-bg {
      padding: 20px;
      background: rgba(0, 0, 0, 0.4);
      position: absolute;
      top: 20vh;
      right: 88px;
      z-index: 999;
      .form {
        width: 320px;
        .login {
          margin-top: 10px;
          width: 100%;
          height: 40px;
        }
      }
    }
    .bg {
      width: 100%;
      height: 75vh;
    }
  }
  .about-psd {
    color: rgba(250, 250, 250, 0.8);
    margin-top: 5px;
    cursor: pointer;
  }
  .bottom {
    height: 10vh;
    font-weight: 500;
    font-size: 16px;
    cursor: pointer;
    .version {
      padding-left: 10px;
      color: #40a9ff;
    }
  }
}
</style>